//全屏 
function fullOpen(){   
  var element= document.documentElement; //若要全屏页面中div，var element= document.getElementById("divID");  
  //IE 10及以下ActiveXObject  
  if (window.ActiveXObject)  
  {  
    var WsShell = new ActiveXObject('WScript.Shell')   
    WsShell.SendKeys('{F11}');   
  }  
  //HTML W3C 提议  
  else if(element.requestFullScreen) {    
    element.requestFullScreen();    
  }  
  //IE11  
  else if(element.msRequestFullscreen) {   
    element.msRequestFullscreen();    
  }  
  // Webkit (works in Safari5.1 and Chrome 15)  
  else if(element.webkitRequestFullScreen ) {    
    element.webkitRequestFullScreen();    
  }   
  // Firefox (works in nightly)  
  else if(element.mozRequestFullScreen) {   
    element.mozRequestFullScreen();    
  }    
}
// 退出全屏
function fullExit(){
    var element= document.documentElement;//若要全屏页面中div，var element= document.getElementById("divID");   
    //IE ActiveXObject  
    if (window.ActiveXObject)  
    {  
      var WsShell = new ActiveXObject('WScript.Shell')   
      WsShell.SendKeys('{F11}');   
    }  
    //HTML5 W3C 提议  
    else if(element.requestFullScreen) {    
      document.exitFullscreen();  
    }  
   //IE 11  
    else if(element.msRequestFullscreen) {    
      document.msExitFullscreen();  
    }  
    // Webkit (works in Safari5.1 and Chrome 15)  
    else if(element.webkitRequestFullScreen ) {    
      document.webkitCancelFullScreen();   
    }   
    // Firefox (works in nightly)  
    else if(element.mozRequestFullScreen) {    
      document.mozCancelFullScreen();    
    }
}
function getQueryString(name) { 
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
  var r = window.location.search.substr(1).match(reg); 
  if (r != null) return unescape(r[2]); return null; 
}
// Cookie
var cookieUtil = {
  setCookie:function(name, value, days) {
      var d = new Date;
      d.setTime(d.getTime() + 24*60*60*1000*days);
      var _value = JSON.stringify(value);
      window.document.cookie = name + "=" + _value + ";path=/;expires=" + d.toGMTString();
  },
  getCookie:function(name){
      var arr=document.cookie.split('; '); //多个cookie值是以; 分隔的，用split把cookie分割开并赋值给数组
      for(var i=0;i<arr.length;i++) //历遍数组
      {
        var arr2=arr[i].split('='); //原来割好的数组是：user=simon，再用split('=')分割成：user simon 这样可以通过arr2[0] arr2[1]来分别获取user和simon 
        if(arr2[0]==name) //如果数组的属性名等于传进来的name
        {
          return JSON.parse(arr2[1]); //就返回属性名对应的值
        }
      }
      return ''; //没找到就返回空
  },
  removeCookie:function(name){
    this.setCookie(name, 1, -1);
  }
}
// localStorage
var storageLocal = {
  set:function(name,value){
    var json_value = JSON.stringify(value);
    window.localStorage.setItem(name,json_value)
  },
  get:function(name){
    var value = window.localStorage.getItem(name);
    if(value){
      try {
          var value_json = JSON.parse(value);
          if (typeof value_json === 'object') {
              return value_json;
          } else if (typeof value_json === 'number') {
              return value_json;
          }
      } catch(e) {
          return value;
      }
    }else {
        return false;
    }
  },
  remove:function(name){
    window.localStorage.removeItem(name);
  },
  clear:function() {
      window.localStorage.clear();
  },
  getName:function(name){
    var value = this.get("userInfo");
    if(value){
      var data = value.resourceMap;
      return data[name]
    }else{
      return false
    }
  }
}
//formatTime
var formatTime = function(time, format){
  var t = time;
  var tf = function(i){return (i < 10 ? '0' : '') + i};
  return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){
      switch(a){
          case 'yyyy':
              return tf(t.getFullYear());
              break;
          case 'MM':
              return tf(t.getMonth() + 1);
              break;
          case 'mm':
              return tf(t.getMinutes());
              break;
          case 'dd':
              return tf(t.getDate());
              break;
          case 'HH':
              return tf(t.getHours());
              break;
          case 'ss':
              return tf(t.getSeconds());
              break;
      }
  })
}
// 百度地图坐标 批量转化(baidu API)
function coordinates(points,callback){
  var convertor = new BMap.Convertor();
  convertor.translate(points, 1, 5, function(data){
    if(data.status === 0) {
      if(typeof callback == "function"){
        callback(data.points)
      }
    }else{
      console.log('status***',data.status)
    }
  })
}
/*
**状态码：
**000000--请求成功
**999999--请求失败
**999902--跳登录界面
**999903--跳空页面
**999904--刷新验证码

*/
// 接口请求统一入口
const apiUrl = {
  wsZ:"ws://39.104.84.146:8083",
  zhangong:'http://39.104.22.46/upgrade/rest',
  // wsW:"ws://39.104.22.46:8030/websocket",
  wsW:"ws://192.168.0.62:8030/websocket",
  wangtao:'http://192.168.62:8030',
  // wangtao:'http://39.104.22.46:8030',
}
function resourceRequst(url,data,callback,isLoading){
  isLoading = isLoading?false:true;
  if(isLoading){
      vm.$loading({
        background: 'rgba(0, 0, 0, 0.6)'
      });
  }
  vm.$http.post(apiUrl.wangtao+url,data,{
    withCredentials: true
  })
  .then(function(response){
    if (typeof callback === 'function') {
      switch (response.body.code){
        case "000000":
          callback(response.body)
        break;
        case "999999":
          vm.$message({
            showClose: true,
            message: response.body.msg,
            duration:3000,
            type: 'error'
          });
        break;
        case "999901":
          vm.$message({
            showClose: true,
            message: response.body.msg,
            duration:0,
            type: 'error'
          });
        break;
        case "999902":
          vm.$message({
            showClose: true,
            message: response.body.msg,
            duration:1000,
            type: 'error',
            onClose:function(){
              window.parent.location.href = "./user-login.html"
            }
          });
        break;
        case "999903":
          vm.$message({
            showClose: true,
            message: response.body.msg,
            duration:1000,
            type: 'error',
            onClose:function(){
              window.parent.location.href = "./empty_page.html"
            }
          });
        break;
        case "999904":
          vm.$message({
            showClose: true,
            message: response.body.msg,
            type: 'error',
            duration:2500
          });
          setTimeout(function(){
            callback(response.body.code)
          }, 500);
        break;
        default:
        console.log("default:",response.body)
      }
      if(isLoading){
        vm.$loading().close()
      }
    }
  },function(res){
    console.table(res.body)
    vm.$message({
      showClose: true,
      message: 'status:'+res.body.status+'；error:'+res.body.error,
      duration:0,
      type: 'error'
    });
    if(isLoading){
      vm.$loading().close()
    }
  })
}

// 角色下拉框
function getSelectRole(back){
  resourceRequst("/role/queryAllRoles",{},function(res){
    if(typeof back === 'function'){
      back(res)
    }
  },true)
}

// 正则验证
var RE = {
  reName:function(name){//用户名正则，4到16位（字母，数字，下划线，减号）-- 连续重复不能超过三个
    var re = /^[a-zA-Z0-9_-]{4,16}$/;
    var retest = /(.)\1\1/;
    if(re.test(name)){
      var val = !retest.test(name)
      return val
    }else{
      return false
    }

  },
  reEmail:function(email){
    if(email == "") return true
    var re =  /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
    var val = re.test(email);
    return val
  },
  reEmpty:function(arr){
    for( var i in arr){
      if(arr[i] === ""){
        return false
      }
    }
    return true
  }
}

